#!/bin/bash
set -e

cd $(dirname $0)/..

echo Starting rancher server
mkdir -p /var/lib/rancher/k3s/agent/images
grep PodTestImage ./tests/integration/pkg/defaults/defaults.go | cut -f2 -d'"' > /var/lib/rancher/k3s/agent/images/pull.txt
grep MachineProvisionImage ./pkg/settings/setting.go | cut -f4 -d'"' >> /var/lib/rancher/k3s/agent/images/pull.txt
touch /tmp/rancher.log
./scripts/run >/tmp/rancher.log 2>&1 &
PID=$!

# uncomment to get startup logs. Don't leave them on because it slows drone down too
# much
#tail -F /tmp/rancher.log &
#TPID=$!
while ! curl -sf http://localhost:8080/ping; do
    sleep 2
done
while ! kubectl --kubeconfig /etc/rancher/k3s/k3s.yaml rollout status -w -n cattle-system deploy/rancher-webhook; do
    sleep 2
done
#kill $TPID

echo Running tests
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
go test -v -timeout 20m ./tests/integration/pkg/tests/...
